/*
===========================================================================
Copyright (C) 2013-2014 Avotu Briezhaudzetava

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

===========================================================================
*/

#ifndef GUI_MAIN_H
#define GUI_MAIN_H

//
// includes
//
#include <QMainWindow>
#include "logtablemodel.h"
#include "main.h"

//
// namespace: Ui
//
namespace Ui {
class Gui_Main;
}

//
// class: Main (gui)
//
class Gui_Main : public QMainWindow {
    Q_OBJECT
    Q_CLASSINFO( "description", "Precursor logger main activity" )
    Q_PROPERTY( int lastIndex READ lastIndex WRITE setLastIndex )
    Q_PROPERTY( bool locked READ isLocked WRITE setLocked )
    Q_PROPERTY( State currentState READ currentState WRITE toggleWidgetState )
    Q_ENUMS( State )

public:
    explicit Gui_Main( QWidget *parent = 0 );
    ~Gui_Main();
    enum State {
        NoState = -1,
        Add,
        Edit
    };
    State currentState() const { return this->m_state; }
    int lastIndex() const { return this->m_lastIndex; }
    bool isLocked() const { return this->m_locked; }

public slots:
    void initialize();

signals:
    void precursorListChanged();

private slots:
    void toggleWidgetState( State state = NoState );
    void toggleWidgetVisibility( bool visible = true );
    void on_buttonAccept_clicked();
    void on_buttonAdd_clicked() { this->toggleWidgetState( Add ); }
    void on_buttonEdit_clicked() { this->toggleWidgetState( Edit ); }
    void on_buttonRemove_clicked();
    void fillPrecursors();
    void fillLogs();
    void fillUnits();
    void on_actionQuit_triggered() { m.shutdown(); }
    void on_actionPrecursors_triggered();
    void setLastIndex( int index = -1 ) { this->m_lastIndex = index; }
    void resetLastIndex() { this->m_lastIndex = -1; }
    void setLocked( bool locked = true ) { this->m_locked = locked; }
    void setUnlocked( bool unlocked = true ) { this->m_locked = !unlocked; }
    void on_comboPrecursors_currentIndexChanged( int index ) { if ( !this->isLocked()) this->setLastIndex( index ); }
    void clearItemState();
    void readItemState();
    void saveItemState();
    void on_actionAbout_triggered();
    void on_actionUsers_triggered();

protected:
    virtual void closeEvent( QCloseEvent *ePtr ) { QMainWindow::closeEvent( ePtr ); m.shutdown(); }

private:
    Ui::Gui_Main *ui;
    LogTableModel *tableModel;
    int m_lastIndex;
    bool m_locked;
    State m_state;
};

#endif // GUI_MAIN_H
